﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ActiveStory.IO.XmlElementNames;

namespace ActiveStory.IO
{
    public class XmlActiveStoryPageReader : IActiveStoryPageReader
    {
        #region IActiveStoryPageReader Members
        /// <summary>
        /// Parses the XML page file at the given absolute path.
        /// Returns:
        /// A string array with the following:
        /// 0:PageName, 1: PageNumber, 2:ButtonCounter
        /// 3:XamlFileLocation, 4:ThumbnailFileLocation
        /// 5: ButtonToPageMapFileLocation
        /// </summary>
        /// <param name="filename">Page file name.</param>
        /// <returns>A string array with the following:
        /// 0:PageName, 1: PageNumber, 2:ButtonCounter
        /// 3:XamlFileLocation, 4:ThumbnailFileLocation
        /// 5: ButtonToPageMapFileLocation
        /// </returns>
        public string[] ParseFile(string filename)
        {
            string[] result=new string[6];
            XmlReaderSettings xrs = new XmlReaderSettings();
            xrs.CloseInput = true;
            XmlReader xr = XmlReader.Create(filename, xrs);

            xr.ReadStartElement(PageFileXmlNames.RootElement);
            
            xr.ReadStartElement(PageFileXmlNames.PageNameElement);
            result[0]= xr.ReadContentAsString();
            xr.ReadEndElement();

            xr.ReadStartElement(PageFileXmlNames.PageNumberElement);
            result[1] = xr.ReadContentAsString();
            xr.ReadEndElement();
            
            xr.ReadStartElement(PageFileXmlNames.ThumbnailFileLocElement);
            result[4] = xr.ReadContentAsString();
            xr.ReadEndElement();
            
            xr.ReadStartElement(PageFileXmlNames.XamlFileLocElement);
            result[3] = xr.ReadContentAsString();
            xr.ReadEndElement();

            xr.ReadStartElement(PageFileXmlNames.ButtonMapFileLocElement);
            result[5] = xr.ReadContentAsString();
            xr.ReadEndElement();

            xr.ReadStartElement(PageFileXmlNames.ButtonCounterElement);
            result[2] = xr.ReadContentAsString();
            xr.ReadEndElement();
            
            xr.ReadEndElement();
            xr.Close();
            return result;
        }

        #endregion
    }
}
